
const a = 6378245.0
const ee = 0.00669342162296594323
const X_PI = Math.PI * 3000.0 / 180.0
/**
 * GCJ02转WGS84
 * @param {*} lon 
 * @param {*} lat 
 * @returns 
 */
function gcj02towgs84(lon, lat) {
  if (out_of_china(lon, lat)) {
    return [lon, lat]
  } else {
    var dlat = transformlat(lon - 105.0, lat - 35.0)
    var dlon = transformlng(lon - 105.0, lat - 35.0)
    var radlat = lat / 180.0 * Math.PI
    var magic = Math.sin(radlat)
    magic = 1 - ee * magic * magic
    var sqrtmagic = Math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * Math.PI)
    dlon = (dlon * 180.0) / (a / sqrtmagic * Math.cos(radlat) * Math.PI)
    var mglon = lon + dlon
    var mlat = lat + dlat
    return [lon * 2 - mglon, lat * 2 - mlat]
  }
}

function transformlat(lon, lat) {
  var ret = -100.0 +
    2.0 * lon +
    3.0 * lat +
    0.2 * lat * lat +
    0.1 * lon * lat +
    0.2 * Math.sqrt(Math.abs(lon))
  ret += (20.0 * Math.sin(6.0 * lon * Math.PI) +
    20.0 * Math.sin(2.0 * lon * Math.PI)) * 2.0 / 3.0
  ret += (20.0 * Math.sin(lat * Math.PI) + 40.0 *
    Math.sin(lat / 3.0 * Math.PI)) * 2.0 / 3.0
  ret += (160.0 * Math.sin(lat / 12.0 * Math.PI) + 320 *
    Math.sin(lat * Math.PI / 30.0)) * 2.0 / 3.0
  return ret
}

function transformlng(lon, lat) {
  var ret = 300.0 + lon +
    2.0 * lat +
    0.1 * lon * lon +
    0.1 * lon * lat +
    0.1 * Math.sqrt(Math.abs(lon))
  ret += (20.0 * Math.sin(6.0 * lon * Math.PI) +
    20.0 * Math.sin(2.0 * lon * Math.PI)) * 2.0 / 3.0
  ret += (20.0 * Math.sin(lon * Math.PI) + 40.0 *
    Math.sin(lon / 3.0 * Math.PI)) * 2.0 / 3.0
  ret += (150.0 * Math.sin(lon / 12.0 * Math.PI) + 300.0 *
    Math.sin(lon / 30.0 * Math.PI)) * 2.0 / 3.0
  return ret
}

function out_of_china(lon, lat) {
  return (lon < 72.004 || lon > 137.8347 || lat < 0.8293 || lat > 55.8271)
}

/**
 * BD09toGCJ02
 * @param {*} lon 
 * @param {*} lat 
 * @returns 
 */
function BD09toGCJ02(lon, lat) {
  var x = lon - 0.0065
  var y = lat - 0.006
  var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI)
  var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI)
  var gg_lng = z * Math.cos(theta)
  var gg_lat = z * Math.sin(theta)
  return [gg_lng, gg_lat]
}
/**
 * WGS84转百度
 * @param {*} lon 
 * @param {*} lat 
 * @returns 
 */
function WGS84toBD09(lon, lat) {
  var gcj02 = wgs84togcj02(lon, lat)
  var bd09 = GCJ02toBD09(gcj02[0], gcj02[1])
  return bd09
}

function wgs84togcj02(lng, lat) {
  if (out_of_china(lng, lat)) {
      return [lng, lat]
  }
  else {
      var dlat = transformlat(lng - 105.0, lat - 35.0);
      var dlng = transformlng(lng - 105.0, lat - 35.0);
      var radlat = lat / 180.0 * Math.PI;
      var magic = Math.sin(radlat);
      magic = 1 - ee * magic * magic;
      var sqrtmagic = Math.sqrt(magic);
      dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * Math.PI);
      dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * Math.PI);
      var mglat = lat + dlat;
      var mglng = lng + dlng;
      return [mglng, mglat]
  }
}

function GCJ02toBD09(lng, lat) {
  var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI);
  var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI);
  var bd_lng = z * Math.cos(theta) + 0.0065;
  var bd_lat = z * Math.sin(theta) + 0.006;
  return [bd_lng, bd_lat]
}

/**
 * 百度转wgs84
 * @param {*} lon 
 * @param {*} lat 
 * @returns 
 */
function BD09toWGS84(lon, lat) {
  var gcj02 = BD09toGCJ02(lon, lat)
  var wgs84 = gcj02towgs84(gcj02[0], gcj02[1])
  return wgs84
}

export { WGS84toBD09, BD09toWGS84, gcj02towgs84, BD09toGCJ02, GCJ02toBD09 }

